package com.jy.adscm.operation.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.base.bean.*;
import com.jy.adscm.operation.base.mapper.mysql.TaskManagerMapper;
import com.jy.adscm.operation.base.mapper.mysql.TaskManagerdetailMapper;
import com.jy.adscm.operation.base.service.TaskManagerAnalysisService;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zyl
 * @Date 2019/1/11 -16:48
 */
@Service
public class TaskManagerAnalysisServiceImpl implements TaskManagerAnalysisService {
    @Autowired
    TaskManagerdetailMapper taskManagerdetailMapper;
    @Autowired
    TaskManagerMapper taskManagerMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public List<TaskManagerAnalysis> queryTaskManagerAnalysis(String userId, String startTime, String endTime) {
        List<TaskManagerAnalysis> taskManagerAnalysisList=new ArrayList<>();
        Wrapper<TaskManagerdetail> wrapper =new EntityWrapper<>();
        wrapper.eq("creator",userId);
        wrapper.ge("start_date",startTime);
        wrapper.le("start_date",endTime);
        List<TaskManagerdetail> taskManagerdetails = taskManagerdetailMapper.selectList(wrapper);
        for (TaskManagerdetail taskManagerdetail : taskManagerdetails) {
            TaskManager taskManager = taskManagerMapper.selectById(taskManagerdetail.getTaskId());
            TaskManagerAnalysis taskManagerAnalysis =new TaskManagerAnalysis();
            taskManagerAnalysis.setId(taskManagerdetail.getId());
            taskManagerAnalysis.setStartDate(taskManagerdetail.getStartDate());
            if(AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getEndDate(),null)){
                taskManagerAnalysis.setEndDate(taskManagerdetail.getEndDate());
            }else {
                //如果没有结束时间就在开始时间上加一分钟
                Date startDate = taskManagerdetail.getStartDate();
                long time = startDate.getTime()+5*60*1000;
                taskManagerAnalysis.setEndDate(new Date(time));
            }

            taskManagerAnalysis.setTaskDescription(taskManagerdetail.getTaskDescription());
            taskManagerAnalysis.setCreatTime(taskManagerdetail.getCreatTime());
            taskManagerAnalysis.setCreatDate(AdscmDateUtils.getFormateDate(taskManagerdetail.getStartDate()));
            taskManagerAnalysis.setIsNowTime(taskManagerdetail.getIsNowTime());
          if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getManagerAdvice(),null)){
              taskManagerAnalysis.setManagerAdvice(taskManagerdetail.getManagerAdvice());
          }
          if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getManager(),null)){
              JyUser jyUser = jyUserMapper.selectById(taskManagerdetail.getManager());
              taskManagerAnalysis.setManagerName(jyUser.getName());
          }
          if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getAdviceCreattime(),null)){
              taskManagerAnalysis.setAdviceCreattime(taskManagerdetail.getAdviceCreattime());
          }
            //每日任务默认完成了
            if ("1".equals(taskManager.getIsEveryDayTask())){
                taskManagerAnalysis.setState("1");
            }else {
                //判断任务是否结束
                if (taskManager.getTaskState()==0){
                         if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getEndDate(),null)){
                             if ((taskManager.getFinishDate().getTime()-taskManager.getEndDate().getTime())<0){
                                 taskManagerAnalysis.setState("-1");
                             }else{
                                 taskManagerAnalysis.setState("1");
                             }
                         }else {
                             if ((taskManager.getFinishDate().getTime()-new Date().getTime())<0){
                                 taskManagerAnalysis.setState("-1");
                             }else{
                                 taskManagerAnalysis.setState("0");
                             }
                         }
                }else {
                   //任务结束
                    System.err.println(taskManager.getId());
                    if ((taskManager.getFinishDate().getTime() - taskManager.getEndDate().getTime())<0){
                        taskManagerAnalysis.setState("-1");
                    }else {
                        taskManagerAnalysis.setState("1");
                    }
                }
            }
            taskManagerAnalysisList.add(taskManagerAnalysis);

        }
        return taskManagerAnalysisList;
    }

    @Override
    public List<TaskManagerAnalysisPieChart> queryPieChartTime(String userId, String startTime, String endTime) {
        List<TaskManagerAnalysisPieChart> taskManagerAnalysisPieCharts= new ArrayList<>();

        long totalTime=0;
             Wrapper<TaskManager> wrapper =new EntityWrapper<>();
             wrapper.eq("performer",userId);
             wrapper.ge("create_date",startTime);
             wrapper.le("create_date",endTime);
        List<TaskManager> taskManagers = taskManagerMapper.selectList(wrapper);
        for (TaskManager taskManager : taskManagers) {
            long taskTotalTime=0;
            TaskManagerAnalysisPieChart taskManagerAnalysisPieChart=new TaskManagerAnalysisPieChart();
            taskManagerAnalysisPieChart.setId(taskManager.getId());
            //非每日任务
            if ("0".equals(taskManager.getIsEveryDayTask())){
                taskManagerAnalysisPieChart.setTaskName(taskManager.getTaskName());
                taskManagerAnalysisPieChart.setTaskContent(taskManager.getTaskContent());
                taskManagerAnalysisPieChart.setCreateDate(taskManager.getCreateDate());
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getCreator(),null)){
                    JyUser jyUser = jyUserMapper.selectById(taskManager.getCreator());
                    taskManagerAnalysisPieChart.setCreatorName(jyUser.getName());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getStartDate(),null)){
                    taskManagerAnalysisPieChart.setStartDate(taskManager.getStartDate());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getEndDate(),null)){
                    taskManagerAnalysisPieChart.setEndDate(taskManager.getEndDate());
                }

            }else {
                //每日任务
                taskManagerAnalysisPieChart.setTaskName(taskManager.getTaskName()+" "+AdscmDateUtils.getNowOnlyDateFomat1(taskManager.getFinishDate()));
                taskManagerAnalysisPieChart.setTaskContent(taskManager.getTaskName()+" "+AdscmDateUtils.getNowOnlyDateFomat1(taskManager.getFinishDate()));
                taskManagerAnalysisPieChart.setCreateDate(taskManager.getCreateDate());
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getCreator(),null)){
                    JyUser jyUser = jyUserMapper.selectById(taskManager.getCreator());
                    taskManagerAnalysisPieChart.setCreatorName(jyUser.getName());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getStartDate(),null)){
                    taskManagerAnalysisPieChart.setStartDate(taskManager.getStartDate());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getEndDate(),null)){
                    taskManagerAnalysisPieChart.setEndDate(taskManager.getEndDate());
                }else {
                    taskManagerAnalysisPieChart.setEndDate(taskManager.getFinishDate());
                }
            }
            //根据id找到明细信息计算时间
           Wrapper<TaskManagerdetail> wrapper1=new EntityWrapper<>();
            wrapper1.eq("task_id",taskManager.getId());
            List<TaskManagerdetail> taskManagerdetails = taskManagerdetailMapper.selectList(wrapper1);
            for (TaskManagerdetail taskManagerdetail : taskManagerdetails) {
                Date startDate = taskManagerdetail.getStartDate();
                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getEndDate(),null)){
                    taskTotalTime=taskTotalTime+(taskManagerdetail.getEndDate().getTime()-startDate.getTime());
                }else {
                    taskTotalTime=taskTotalTime+60*1000;
                }

            }
            taskManagerAnalysisPieChart.setUseTime(taskTotalTime);
            taskManagerAnalysisPieCharts.add(taskManagerAnalysisPieChart);
            totalTime=totalTime+taskTotalTime;
        }
        for (TaskManagerAnalysisPieChart taskManagerAnalysisPieChart : taskManagerAnalysisPieCharts) {
            taskManagerAnalysisPieChart.setTotalTime(totalTime);
            long useTime = taskManagerAnalysisPieChart.getUseTime();
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);
            float  result= ((float)useTime / (float) totalTime)*100;
            String format = numberFormat.format(result);
            taskManagerAnalysisPieChart.setPercent(String.valueOf(format));
        }
        return taskManagerAnalysisPieCharts;
    }

    @Override
    public List<JyUser> queryUser() {
        Wrapper<JyUser> wrapper=new EntityWrapper<>();
        wrapper.eq("enabled",1);
        wrapper.setSqlSelect("id","name");
        List<JyUser> jyUsers = jyUserMapper.selectList(wrapper);
        return jyUsers;
    }

    @Override
    public List<TaskManagerAnalysisSelfWorthPieChart> querySelfWorthPieChart(String userId, String startTime, String endTime) {
        List<TaskManagerAnalysisSelfWorthPieChart> TaskManagerAnalysisSelfWorthPieCharts= new ArrayList<>();
        Wrapper<TaskManager> wrapper =new EntityWrapper<>();
        wrapper.eq("performer",userId);
        wrapper.ge("create_date",startTime);
        wrapper.le("create_date",endTime);
        wrapper.eq("isEveryDayTask","0");
        List<TaskManager> taskManagers = taskManagerMapper.selectList(wrapper);
        if (taskManagers.size()>0){

            int excellent=0;
            int good=0;
            int bad=0;
            for (TaskManager taskManager : taskManagers) {

                if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getTaskSelfEvaluation(),null)){

                       if ("3".equals(taskManager.getTaskSelfEvaluation())){
                           excellent++;

                       }
                       if ("2".equals(taskManager.getTaskSelfEvaluation())){
                           good++;

                       }
                       if ("1".equals(taskManager.getTaskSelfEvaluation())){
                           bad++;

                       }
                }

            }
            TaskManagerAnalysisSelfWorthPieChart taskManagerAnalysisSelfWorthPieChart =new TaskManagerAnalysisSelfWorthPieChart();
            taskManagerAnalysisSelfWorthPieChart.setType("优秀");
            taskManagerAnalysisSelfWorthPieChart.setUse(String.valueOf(excellent));
            taskManagerAnalysisSelfWorthPieChart.setTotal(String.valueOf(taskManagers.size()));
            TaskManagerAnalysisSelfWorthPieCharts.add(taskManagerAnalysisSelfWorthPieChart);
            TaskManagerAnalysisSelfWorthPieChart taskManagerAnalysisSelfWorthPieChart1 =new TaskManagerAnalysisSelfWorthPieChart();
            taskManagerAnalysisSelfWorthPieChart1.setType("良");
            taskManagerAnalysisSelfWorthPieChart1.setUse(String.valueOf(good));
            taskManagerAnalysisSelfWorthPieChart1.setTotal(String.valueOf(taskManagers.size()));
            TaskManagerAnalysisSelfWorthPieCharts.add(taskManagerAnalysisSelfWorthPieChart1);
            TaskManagerAnalysisSelfWorthPieChart taskManagerAnalysisSelfWorthPieChart2=new TaskManagerAnalysisSelfWorthPieChart();
            taskManagerAnalysisSelfWorthPieChart2.setType("差");
            taskManagerAnalysisSelfWorthPieChart2.setUse(String.valueOf(bad));
            taskManagerAnalysisSelfWorthPieChart2.setTotal(String.valueOf(taskManagers.size()));
            TaskManagerAnalysisSelfWorthPieCharts.add(taskManagerAnalysisSelfWorthPieChart2);
            TaskManagerAnalysisSelfWorthPieChart taskManagerAnalysisSelfWorthPieChart3=new TaskManagerAnalysisSelfWorthPieChart();
            taskManagerAnalysisSelfWorthPieChart3.setType("未评价");
            taskManagerAnalysisSelfWorthPieChart3.setUse(String.valueOf(taskManagers.size()-bad-excellent-good));
            taskManagerAnalysisSelfWorthPieChart3.setTotal(String.valueOf(taskManagers.size()));
            TaskManagerAnalysisSelfWorthPieCharts.add(taskManagerAnalysisSelfWorthPieChart3);

        }

        return TaskManagerAnalysisSelfWorthPieCharts;
    }

    @Override
    public List<TaskManagerAnalysis> queryTaskManager(String userId, String startTime, String endTime) {
        List<TaskManagerAnalysis> taskManagerAnalysisList=new ArrayList<>();
        Wrapper<TaskManagerdetail> wrapper =new EntityWrapper<>();
        wrapper.eq("creator",userId);
        wrapper.ge("start_date",startTime);
        wrapper.le("start_date",endTime);
        wrapper.orderBy("start_date",true);
        List<TaskManagerdetail> taskManagerdetails = taskManagerdetailMapper.selectList(wrapper);
        for (TaskManagerdetail taskManagerdetail : taskManagerdetails) {
            TaskManager taskManager = taskManagerMapper.selectById(taskManagerdetail.getTaskId());
            TaskManagerAnalysis taskManagerAnalysis =new TaskManagerAnalysis();
            taskManagerAnalysis.setId(taskManagerdetail.getId());
            taskManagerAnalysis.setStartDate(taskManagerdetail.getStartDate());
            if(AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getEndDate(),null)){
                taskManagerAnalysis.setEndDate(taskManagerdetail.getEndDate());
            }else {
                //如果没有结束时间就在开始时间上加一分钟
                Date startDate = taskManagerdetail.getStartDate();
                long time = startDate.getTime()+5*60*1000;
                taskManagerAnalysis.setEndDate(new Date(time));
            }
            taskManagerAnalysis.setTaskDescription(taskManagerdetail.getTaskDescription());
            taskManagerAnalysis.setCreatTime(taskManagerdetail.getCreatTime());
            taskManagerAnalysis.setCreatDate(AdscmDateUtils.getFormateDate(taskManagerdetail.getStartDate()));
            taskManagerAnalysis.setIsNowTime(taskManagerdetail.getIsNowTime());
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getManagerAdvice(),null)){
                taskManagerAnalysis.setManagerAdvice(taskManagerdetail.getManagerAdvice());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getManager(),null)){
                JyUser jyUser = jyUserMapper.selectById(taskManagerdetail.getManager());
                taskManagerAnalysis.setManagerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getAdviceCreattime(),null)){
                taskManagerAnalysis.setAdviceCreattime(taskManagerdetail.getAdviceCreattime());
            }
            //每日任务默认完成了
            if ("1".equals(taskManager.getIsEveryDayTask())){
                taskManagerAnalysis.setState("0");
            }else {
                taskManagerAnalysis.setState("1");
            }
            Date startDate = taskManagerAnalysis.getStartDate();
            Date endDate = taskManagerAnalysis.getEndDate();
            String datePoor = AdscmDateUtils.getDatePoor(endDate, startDate);
            taskManagerAnalysis.setUseTime(datePoor);
            taskManagerAnalysisList.add(taskManagerAnalysis);

        }

        for (int i = 1; i < taskManagerAnalysisList.size(); i++) {
              //先判断这连个日期是否是在同一天
            TaskManagerAnalysis before = taskManagerAnalysisList.get(i - 1);
            TaskManagerAnalysis after = taskManagerAnalysisList.get(i);

            if (AdscmDateUtils.isSameDay(before.getEndDate(),after.getStartDate())){
                //说明有重叠的地方
                if (before.getEndDate().getTime()-after.getStartDate().getTime()>0){
                    after.setIsRepeat("1");
                }
            }
        }
        return taskManagerAnalysisList;
    }


}
